package com.example.hive;

import org.apache.hadoop.hive.metastore.HiveMetaStoreClient;
import org.apache.hadoop.hive.metastore.api.FieldSchema;
import org.apache.hadoop.hive.metastore.api.MetaException;
import org.apache.thrift.TException;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @Author: leali
 * @Version: 1.0
 * @Date: 2021/11/12-11:30
 * @Description:
 */
public class MetaDataLoader {
    public static String getDatabaseInfo(HiveMetaStoreClient hiveMetaStoreClient, String databaseName) {
        try {
            return hiveMetaStoreClient.getDatabase(databaseName).getLocationUri();
        } catch (TException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static List<String> getTablesByDatabase(HiveMetaStoreClient hiveMetaStoreClient, String databaseName) {
        try {
            return hiveMetaStoreClient.getAllTables(databaseName);
        } catch (MetaException e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    public static Boolean isExistsTable(HiveMetaStoreClient hiveMetaStoreClient,String databaseName,String tableName){
        try {
            return hiveMetaStoreClient.tableExists(databaseName,tableName);
        } catch (TException e) {
            e.printStackTrace();
        }
        return false;
    }
    public static List<FieldSchema> getTableInfo(HiveMetaStoreClient hiveMetaStoreClient, String databaseName, String tableName) {
        try {
            return hiveMetaStoreClient.getTable(databaseName, tableName).getSd().getCols();
        } catch (TException e) {
            e.printStackTrace();
        }
        return null;
    }
    public static Boolean isExistTableColumn(HiveMetaStoreClient hiveMetaStoreClient, String databaseName, String tableName, String columnName) {
        try {
            List<FieldSchema> fieldSchemaList = hiveMetaStoreClient.getFields(databaseName, tableName);
            for (FieldSchema fieldSchema : fieldSchemaList) {
                if (Objects.equals(fieldSchema.getName(), columnName)) {
                    return true;
                }
            }
        } catch (TException e) {
            e.printStackTrace();
        }
        return false;
    }
    public static String getColumnType(HiveMetaStoreClient hiveMetaStoreClient, String databaseName, String tableName, String columnName) {
        try {
            List<FieldSchema> fieldSchemaList = hiveMetaStoreClient.getFields(databaseName, tableName);
            for (FieldSchema fieldSchema : fieldSchemaList) {
                if (Objects.equals(fieldSchema.getName(), columnName)) {
                    return fieldSchema.getType();
                }
            }
        } catch (TException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Boolean isPartitionTable(HiveMetaStoreClient hiveMetaStoreClient, String databaseName, String tableName){
        try {
            return !hiveMetaStoreClient.listPartitionNames(databaseName, tableName, (short) -1).isEmpty();
        } catch (TException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String getMaxPartitionNames(HiveMetaStoreClient hiveMetaStoreClient, String databaseName, String tableName) {
        try {
            List<String> listPartitionNames = hiveMetaStoreClient.listPartitionNames(databaseName, tableName, (short) -1);
            Collections.reverse(listPartitionNames);
            return Objects.requireNonNull(listPartitionNames.get(0));
        } catch (TException e) {
            e.printStackTrace();
        }
        return null;
    }
}
